package org.jeecg.modules.website.controller.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.website.entity.WebSchoolMessage;
import org.jeecg.modules.website.entity.WebWebsiteMenu;
import org.jeecg.modules.website.entity.WebWorkFile;
import org.jeecg.modules.website.service.IWebSchoolMessageService;
import org.jeecg.modules.website.service.IWebWorkFileService;
import org.jeecg.modules.website.vo.WebSchoolMessagePage;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @Description: 学校信息表
 * @Author: jeecg-boot
 * @Date:   2024-08-02
 * @Version: V1.0
 */
@Api(tags="网站API-校园信息相关接口")
@RestController
@RequestMapping("/api/web/webSchoolMessage")
@Slf4j
public class WebSchoolMessageApiController extends JeecgController<WebSchoolMessage, IWebSchoolMessageService> {
    @Autowired
    private IWebSchoolMessageService webSchoolMessageService;
    @Autowired
    private IWebWorkFileService webWorkFileService;

    /**
     * 分页列表查询
     *
     * @param webSchoolMessage
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "学校信息表-分页列表查询")
    @ApiOperation(value="学校信息表-分页列表查询", notes="学校信息表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<WebSchoolMessage>> queryPageList(WebSchoolMessage webSchoolMessage,
                                                         @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                         @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                         HttpServletRequest req) {
        QueryWrapper<WebSchoolMessage> queryWrapper = QueryGenerator.initQueryWrapper(webSchoolMessage, req.getParameterMap());
        Page<WebSchoolMessage> page = new Page<WebSchoolMessage>(pageNo, pageSize);
        queryWrapper.lambda().select(WebSchoolMessage.class, i -> !i.getProperty().equals("content"));
        queryWrapper.eq("publish_status",1); // 只查询已发布的
        queryWrapper.last(" order by publish_time desc"); // 根据发布时间排序
        IPage<WebSchoolMessage> pageList = webSchoolMessageService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "学校信息表-通过id查询")
    @ApiOperation(value="学校信息表-通过id查询", notes="学校信息表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<WebSchoolMessage> queryById(@RequestParam(name="id",required=true) String id) {
        WebSchoolMessage webSchoolMessage = webSchoolMessageService.getById(id);
        if(webSchoolMessage==null) {
            return Result.error("未找到对应数据");
        }
        // 每加载一次 阅读量+1
        webSchoolMessage.setReadNum(webSchoolMessage.getReadNum()+1);
        webSchoolMessageService.updateById(webSchoolMessage);
        return Result.OK(webSchoolMessage);
    }
    @ApiOperation(value="专业信息-首页封面", notes="专业信息-首页封面")
    @GetMapping(value = "/queryMajor")
    public Result<List<WebSchoolMessage>> queryMajor() {
        List<WebSchoolMessage> webSchoolMessage = webSchoolMessageService.selectMajor();
        return Result.OK(webSchoolMessage);
    }
    @ApiOperation(value="文化课介绍信息详情", notes="文化课介绍信息详情")
    @GetMapping(value = "/queryLetter")
    public Result<WebSchoolMessage> queryLetter(@RequestParam(name="majorName",required=true) String majorName) {
        WebSchoolMessage webSchoolMessage = webSchoolMessageService.selectMajorName(majorName);
        return Result.OK(webSchoolMessage);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "附件表通过主表ID查询")
    @ApiOperation(value="附件表主表ID查询", notes="附件表-通主表ID查询")
    @GetMapping(value = "/queryWebWorkFileByMainId")
    public Result<List<WebWorkFile>> queryWebWorkFileListByMainId(@RequestParam(name="mainId",required=true) String id) {
        List<WebWorkFile> webWorkFileList = webWorkFileService.selectByMainId(id);
        return Result.OK(webWorkFileList);
    }
    //@AutoLog(value = "附件表通过主表ID查询PNG类型")
    @ApiOperation(value="跳转轮播图", notes="跳转轮播图")
    @GetMapping(value = "/queryWebWorkFilePng")
    public Result<List<WebWorkFile>> queryIMG(@RequestParam(name="mainId",required=true) String mainId) {
        List<WebWorkFile> webWorkFileList = webWorkFileService.selectByMainIdAndFileType(mainId, "PNG");
        return Result.OK(webWorkFileList);
    }
    //@AutoLog(value = "附件表通过主表ID查询PNG类型")
    @ApiOperation(value="专业轮播图", notes="专业轮播图")
    @GetMapping(value = "/queryWebWorkFilePng/{mainId}")
    public Result<List<WebWorkFile>> queryWebWorkFileListByMainIdAndFileType(@PathVariable String mainId) {
        List<WebWorkFile> webWorkFileList = webWorkFileService.selectByMainIdAndFileType(mainId, "PNG");
        return Result.OK(webWorkFileList);
    }


    //@AutoLog(value = "附件表通过主表ID查询PNG类型")
    @ApiOperation(value="党群工作轮播图", notes="党群工作轮播图")
    @GetMapping(value = "/querypartyImg")
    public Result<List<WebWorkFile>> querypartyImg(String id) {
        Long   ids=1866084198808281089L;
        id=Long.toString(ids);
        List<WebWorkFile> webWorkFileList = webWorkFileService.selectByMainIdAndFileType(id, "PNG");
        return Result.OK(webWorkFileList);
    }
    //@AutoLog(value = "附件表通过主表ID查询PNG类型")
    @ApiOperation(value="校园生活轮播图", notes="校园生活轮播图")
    @GetMapping(value = "/querylifeImg")
    public Result<List<WebWorkFile>> querylifeImg(String id) {
        Long   ids=1866084095385133057L;
        id=Long.toString(ids);
        List<WebWorkFile> webWorkFileList = webWorkFileService.selectByMainIdAndFileType(id, "PNG");
        return Result.OK(webWorkFileList);
    }
    //@AutoLog(value = "附件表通过主表ID查询PNG类型")
    @ApiOperation(value="关于学校轮播图", notes="关于学校轮播图")
    @GetMapping(value = "/querySchoolImg")
    public Result<List<WebWorkFile>> querySchoolImg(String id) {
        Long   ids=1874759042026999810L;
        id=Long.toString(ids);
        List<WebWorkFile> webWorkFileList = webWorkFileService.selectByMainIdAndFileType(id, "PNG");
        return Result.OK(webWorkFileList);
    }

    @ApiOperation(value="学校信息表-首页校园新闻轮播", notes="学校信息表-首页校园新闻轮播")
    @GetMapping(value = "/listImg")
    public Result<IPage< WebSchoolMessage>> queryPageListImg( WebSchoolMessage  webSchoolMessage,
                                                              @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                              @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                              HttpServletRequest req) {
        pageSize = 5;
        QueryWrapper<WebSchoolMessage> queryWrapper = QueryGenerator.initQueryWrapper(webSchoolMessage, req.getParameterMap());
        queryWrapper.eq("publish_status",1); // 只查询已发布的
        queryWrapper.eq("category","sys_school_news"); // 只查询新闻动态
        queryWrapper.isNotNull("cover_image");
        //queryWrapper.eq("priority","H"); // 优先级高

        queryWrapper.last(" order by publish_time desc"); // 根据发布时间排序
        queryWrapper.lambda().select(WebSchoolMessage.class, i -> !i.getProperty().equals("content"));
        Page<WebSchoolMessage> page = new Page<WebSchoolMessage>(pageNo, pageSize);
        IPage<WebSchoolMessage> pageList = webSchoolMessageService.page(page, queryWrapper);
        return Result.OK(pageList);
    }



    @ApiOperation(value="悬浮窗", notes="学校悬浮窗")
    @GetMapping(value = "/listWindow")
    public Result<IPage< WebSchoolMessage>> queryPageListWindow( WebSchoolMessage  webSchoolMessage,
                                                              @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                              @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                              HttpServletRequest req) {
        pageSize = 5;
        QueryWrapper<WebSchoolMessage> queryWrapper = QueryGenerator.initQueryWrapper(webSchoolMessage, req.getParameterMap());
        queryWrapper.eq("publish_status",1); // 只查询已发布的
        queryWrapper.eq("category","sys_school_news"); // 只查询新闻动态
        queryWrapper.eq("sus_win","1");//悬浮状态
        queryWrapper.select("id", "img");
        Page<WebSchoolMessage> page = new Page<WebSchoolMessage>(pageNo, pageSize);

        IPage<WebSchoolMessage> pageList = webSchoolMessageService.page(page, queryWrapper);

        return Result.OK(pageList);
    }

}
